// Invoked on the commit-msg git hook by yorkie.
const chalk = require('chalk')
const chalkCtx = new chalk.Instance({level: 1})
const msgPath = process.env.HUSKY_GIT_PARAMS
const msg = require('fs')
  .readFileSync(msgPath, 'utf-8')
  .trim()

const commitRE = /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/

if (!commitRE.test(msg)) {
  console.log()
  console.error(
      `   ${chalkCtx.bgRed.white(' ERROR ')} ${chalkCtx.red(
      `invalid commit message format.`
    )}\n` +
      chalkCtx.red(
      `    Commit msg: ${msg}\n\n`  
      ) +
      chalkCtx.red(
      `    Proper commit message format is required for automated changelog generation. Examples:\n`
      ) +
      `    ${chalkCtx.green(`feat: 增加新的特性，新功能`)}\n` +
      `    ${chalkCtx.green(`fix: 修复bug`)}\n` +
      `    ${chalkCtx.green(`perf: 优化相关，提高性能的代码`)}\n` +
      `    ${chalkCtx.green(`refactor: 重构（即不是新增功能，也不是修改bug的代码变动）`)}\n` +
      `    ${chalkCtx.green(`style: 不影响代码含义的修改，比如空格、格式化、缺失的分号等`)}\n` +
      `    ${chalkCtx.green(`test: 增加测试`)}\n` +
      `    ${chalkCtx.green(`docs: 对文档进行了修改`)}\n` +
      `    ${chalkCtx.green(`build: 对构建系统或者外部依赖进行了修改`)}\n` +
      `    ${chalkCtx.green(`ci: 对CI配置文件或者脚本进行了修改`)}\n\n` +
      chalkCtx.red(`  See https://github.com/conventional-changelog/commitlint.\n`)
  )
  process.exit(1)
}
